﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GolfLeague.EditModels;
using GolfLeague.Models;
using GolfLeague.ViewModels;

namespace GolfLeague.Controllers
{
    public class ScheduleController : Controller
    {
        ScheduleInterface scheduleInterface = new ScheduleInterface();
        RoundInterface roundInterface = new RoundInterface();
        MatchInterface matchInterface = new MatchInterface();
        HandicapInterface handicapInterface = new HandicapInterface();

        //
        // GET: /Schedule/

        [Authorize(Roles = "Administrator")]
        public ActionResult Index()
        {
            return View(scheduleInterface.GetSchedulesEditModel());
        }

        //
        // POST: /Schedule/
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Index(SchedulesEditModel schedulesEditModel)
        {
            if (ModelState.IsValid)
            {
                scheduleInterface.AddSchedule(schedulesEditModel.NewSchedule);
                scheduleInterface.Save();

                return RedirectToAction("Index");
            }
            Schedule schedule = schedulesEditModel.NewSchedule;
            schedulesEditModel = scheduleInterface.GetSchedulesEditModel();
            schedulesEditModel.NewSchedule = schedule;
            return View(schedulesEditModel);
        }

        //
        // GET: /Schedule/ScheduleEdit/

        [Authorize(Roles = "Administrator")]
        public ActionResult ScheduleEdit(int id)
        {
            return View("Index", scheduleInterface.GetSchedulesEditModel(id));
        }

        //
        // POST: /Schedule/ScheduleEdit/
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult ScheduleEdit(SchedulesEditModel schedulesEditModel)
        {
            if (ModelState.IsValid)
            {
                var updateSchedule = scheduleInterface.GetSchedule(schedulesEditModel.NewSchedule.ScheduleID);

                updateSchedule.ScheduleName = schedulesEditModel.NewSchedule.ScheduleName;
                updateSchedule.ScheduleYear = schedulesEditModel.NewSchedule.ScheduleYear;

                if (TryUpdateModel(updateSchedule))
                {
                    scheduleInterface.Save();
                    return RedirectToAction("Index");
                }
            }
            Schedule schedule = schedulesEditModel.NewSchedule;
            schedulesEditModel = scheduleInterface.GetSchedulesEditModel();
            schedulesEditModel.NewSchedule = schedule;
            return View("Index", schedulesEditModel);
        }

        //
        // POST: /Schedule/ScheduleDelete/
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult ScheduleDelete(int scheduleID)
        {
            Schedule schedule = scheduleInterface.GetSchedule(scheduleID);

            scheduleInterface.DeleteSchedule(schedule);
            scheduleInterface.Save();

            return RedirectToAction("Index");
        }

        //
        // GET: /Schedule/ScheduleView/

        public ActionResult ScheduleView(int? id)
        {
            return View(scheduleInterface.GetScheduleViewModel(id));
        }

    }
}
